/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import DAO.util.ConectaBanco;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import model.Chave;


/**
 *
 * @author Alex
 */
public class ChaveDAO {

    public boolean criar(Chave chave) {
        try {

            Connection conexao = ConectaBanco.getConexao();

            String sql = "insert into chave(nome)values (?)";

            PreparedStatement pstmt = conexao.prepareStatement(sql);

            pstmt.setString(1, chave.getNome());
         
            pstmt.execute();
            conexao.close();

            return true;

        } catch (SQLException sql) {
            sql.printStackTrace();
            return false;

        }

    }

    public Chave buscarPorId(int idChave) {

        Connection conexao = null;
        try {
            conexao = ConectaBanco.getConexao();
            String sql = "select * from chave jogo where id= ?";

            PreparedStatement pst = conexao.prepareStatement(sql);

            pst.setInt(1, idChave);
            ResultSet rs = pst.executeQuery();

            rs.next();
            Chave chave = new Chave();
            
            chave.setId(rs.getInt("id"));
            

            conexao.close();
            return chave;

        } catch (Exception sql) {
            System.out.println("Erro ao buscar um aluno" + sql.getMessage());
            return null;
        }

    }

}
